import json
import copy
import math
import base64
import bezier
import requests
import numpy as np
from cv2 import cv2
from matplotlib import pyplot as plt
from skimage.metrics import structural_similarity as ssim


# PLT字体显示
# plt.rcParams['font.sans-serif'] = ['SimHei']    # 设置字体
# plt.rcParams['axes.unicode_minus'] = False      # 字符显示


# 文件路径
# fixedImagePath = 'D:/Desktop/FILE1.JPG'
# movingImagePath = 'D:/Desktop/FILE2.JPG'


'''相似性测度'''
# 【互信息:MI(A,B) = H(A) + H(B) - H(A,B)】
def MI(x, y):
    size = x.shape[-1]
    px = np.histogram(x, 256, (0, 255))[0] / size
    py = np.histogram(y, 256, (0, 255))[0] / size
    hx = -np.sum(px * np.log(px + 1e-8))
    hy = -np.sum(py * np.log(py + 1e-8))

    hxy = np.histogram2d(x, y, 256, [[0, 255], [0, 255]])[0]
    hxy /= (1.0 * size)
    hxy = -np.sum(hxy * np.log(hxy + 1e-8))

    res = hx + hy - hxy
    return res

# 【均方误差:[I(i,j)-K(i,j)]^2 / (m * n)】
def MSE(img_A, img_B):
    err = np.sum((img_A.astype('float') - img_B.astype('float')) ** 2)
    err /= float(img_A.shape[0] * img_A.shape[1])
    return err


'''初始化特征点和轮廓'''
# 获取特征点名称和特征点x、y数组函数
def getLandmarks(ImageBase64):
    # 获取图像的base64编码
    # with open(ImagePath, 'rb') as f:
    #     ImageBase64 = base64.b64encode(f.read())

    # 向网页请求获取特征点
    web_path = 'http://model.deepstomatology.machineilab.org/get_results'
    ImageParams = {'file': ImageBase64, 'model_name': 'huaxi_ceph', 'data_type': 'ceph'}
    ImageRes = requests.post(web_path, data=ImageParams)

    # 将返回的json特征点数据转化为字典形式存储
    ImageDict = json.loads(ImageRes.content)
    # 特征点名称数组
    landmark_names = []
    landmark_names.append("A")
    landmark_names.append("ANS")
    landmark_names.append("Ar")
    landmark_names.append("B")
    landmark_names.append("Ba")
    landmark_names.append("C")
    landmark_names.append("Co")
    landmark_names.append("G")
    landmark_names.append("Gn")
    landmark_names.append("Gns")
    landmark_names.append("Go")
    landmark_names.append("Id")
    landmark_names.append("LI")
    landmark_names.append("LIA")
    landmark_names.append("Li")
    landmark_names.append("Lm")
    landmark_names.append("Ls")
    landmark_names.append("Me")
    landmark_names.append("Mes")
    landmark_names.append("N")
    landmark_names.append("Ns")
    landmark_names.append("Or")
    landmark_names.append("PNS")
    landmark_names.append("Pcd")
    landmark_names.append("Po")
    landmark_names.append("Pog")
    landmark_names.append("Pogs")
    landmark_names.append("Prn")
    landmark_names.append("Pt")
    landmark_names.append("Ptm")
    landmark_names.append("S")
    landmark_names.append("Spr")
    landmark_names.append("Si")
    landmark_names.append("Sn")
    landmark_names.append("UI")
    landmark_names.append("UIA")
    landmark_names.append("Um")
    # 特征点x、y坐标数组
    landmark_x = []
    landmark_y = []
    for [m, n] in ImageDict.values():
        landmark_x.append(m)
        landmark_y.append(n)

    return ImageDict, landmark_names, landmark_x, landmark_y


# 得到辅助点的名称数组函数
def getAuxiliarys():
    auxiliary_names = []
    auxiliary_names.append("G_Ns_middle")
    auxiliary_names.append("Ns_down")
    auxiliary_names.append("Prn_left_up")
    auxiliary_names.append("Prn_Sn_middle")
    auxiliary_names.append("Ls_UI_middle")
    auxiliary_names.append("Mes_C_middle")

    auxiliary_names.append("N_up")
    auxiliary_names.append("N_up_add")
    auxiliary_names.append("N_left")
    auxiliary_names.append("N_left_down_middle")
    auxiliary_names.append("N_down_before")
    auxiliary_names.append("N_down")
    auxiliary_names.append("N_down_middle")
    auxiliary_names.append("N_down_middle_2")

    auxiliary_names.append("Or_up")
    auxiliary_names.append("Or_up_left_middle")
    auxiliary_names.append("Or_left")
    auxiliary_names.append("Or_left_middle")
    auxiliary_names.append("Or_right_middle")
    auxiliary_names.append("Or_right")

    auxiliary_names.append("midPtOr_up")
    auxiliary_names.append("midPtOr_up_mid_mid")
    auxiliary_names.append("midPtOr_mid")
    auxiliary_names.append("midPtOr_mid_midPtOr_mid")
    auxiliary_names.append("midPtOr")
    auxiliary_names.append("midPtOr_down_mid")
    auxiliary_names.append("midPtOr_down")
    auxiliary_names.append("midPtOr_down_right_mid")
    auxiliary_names.append("midPtOr_right")

    auxiliary_names.append("S_up")
    auxiliary_names.append("S_up_mid_mid")
    auxiliary_names.append("S_mid")
    auxiliary_names.append("S_mid_short_right_mid")
    auxiliary_names.append("S_mid_right_mid")  # S短曲线
    auxiliary_names.append("S_right_before")  # S短曲线
    auxiliary_names.append("S_right")  # S短曲线
    auxiliary_names.append("S_short_right")
    auxiliary_names.append("S_short_down")
    auxiliary_names.append("S_short_left")
    auxiliary_names.append("S_short_left_up")
    auxiliary_names.append("S_short_left_left")
    auxiliary_names.append("S_left_Ba_mid")
    auxiliary_names.append("S_left_Ba_before")
    auxiliary_names.append("Ar_up")  # ArBa曲线
    auxiliary_names.append("Ar_up_mid_mid")  # ArBa曲线
    auxiliary_names.append("Ar_mid")  # ArBa曲线
    auxiliary_names.append("Ar_mid_down")  # ArBa曲线
    auxiliary_names.append("Ba_up")  # ArBa曲线

    auxiliary_names.append("Co_after")
    auxiliary_names.append("Co_Pcd_mid")
    auxiliary_names.append("Ar_Go_mid")
    auxiliary_names.append("Go_Me_mid")
    auxiliary_names.append("Go_Me_mid_2")
    auxiliary_names.append("Me_before")

    auxiliary_names.append("Pt_up")
    auxiliary_names.append("Pt_mid")
    auxiliary_names.append("Pt_down")
    auxiliary_names.append("Pt_right")
    auxiliary_names.append("Pt_right_mid")
    auxiliary_names.append("Pt_right_down")

    auxiliary_names.append("Co_right_before")
    auxiliary_names.append("Co_right")
    auxiliary_names.append("Co_right_mid_mid")
    auxiliary_names.append("Co_mid")
    auxiliary_names.append("Co_mid_down_mid")
    auxiliary_names.append("Co_down")
    auxiliary_names.append("Co_down_Ptm_mid")
    auxiliary_names.append("Ptm_up_left_before")
    auxiliary_names.append("Ptm_up")
    auxiliary_names.append("Ptm_up_mid")
    auxiliary_names.append("Ptm_mid")
    auxiliary_names.append("Ptm_mid_down_mid")
    auxiliary_names.append("Ptm_down")

    auxiliary_names.append("PNS_after")
    auxiliary_names.append("PNS_right_before")
    auxiliary_names.append("PNS_right")
    auxiliary_names.append("PNS_ANS_top_before")
    auxiliary_names.append("PNS_ANS_top")
    auxiliary_names.append("ANS_before")

    auxiliary_names.append("PNS_down_after")
    auxiliary_names.append("PNS_mid_before")
    auxiliary_names.append("PNS_mid")
    auxiliary_names.append("PNS_mid_down_mid")
    auxiliary_names.append("PNS_down_before")
    auxiliary_names.append("PNS_down")

    auxiliary_names.append("ANS_left")
    auxiliary_names.append("ANS_after")
    auxiliary_names.append("ANS_A_mid")
    auxiliary_names.append("A_Spr_mid")

    auxiliary_names.append("Me_up")
    auxiliary_names.append("Me_up_after")
    auxiliary_names.append("Me_mid")
    auxiliary_names.append("Me_left")
    auxiliary_names.append("Me_before_left")

    auxiliary_names.append("Id_after")
    auxiliary_names.append("Id_B_mid")
    auxiliary_names.append("Me_before_right")

    auxiliary_names.append("LI_after")
    auxiliary_names.append("Id_after")
    auxiliary_names.append("LIA_before")
    auxiliary_names.append("LIA_left")
    auxiliary_names.append("LIA_Id_mid")
    auxiliary_names.append("Id_left")
    auxiliary_names.append("Id_left_up")
    auxiliary_names.append("Id_LI_mid")
    auxiliary_names.append("LI_before")

    auxiliary_names.append("UI_after")
    auxiliary_names.append("Spr_after")
    auxiliary_names.append("UIA_before")
    auxiliary_names.append("UIA_left")
    auxiliary_names.append("UIA_Spr_mid")
    auxiliary_names.append("Spr_left")
    auxiliary_names.append("Spr_left_down")
    auxiliary_names.append("Spr_UI_mid")
    auxiliary_names.append("UI_before")

    auxiliary_names.append("Lm_clockwise_0")
    auxiliary_names.append("Lm_clockwise_1")
    auxiliary_names.append("Lm_clockwise_2")
    auxiliary_names.append("Lm_clockwise_3")
    auxiliary_names.append("Lm_clockwise_4")
    auxiliary_names.append("Lm_clockwise_5")
    auxiliary_names.append("Lm_clockwise_6")
    auxiliary_names.append("Lm_clockwise_7")
    auxiliary_names.append("Lm_clockwise_8")
    auxiliary_names.append("Lm_clockwise_9")
    auxiliary_names.append("Lm_clockwise_10")
    auxiliary_names.append("Lm_clockwise_11")
    auxiliary_names.append("Lm_clockwise_12")
    auxiliary_names.append("Lm_clockwise_13")
    auxiliary_names.append("Lm_clockwise_14")

    auxiliary_names.append("Um_clockwise_0")
    auxiliary_names.append("Um_clockwise_1")
    auxiliary_names.append("Um_clockwise_2")
    auxiliary_names.append("Um_clockwise_3")
    auxiliary_names.append("Um_clockwise_4")
    auxiliary_names.append("Um_clockwise_5")
    auxiliary_names.append("Um_clockwise_6")
    auxiliary_names.append("Um_clockwise_7")
    auxiliary_names.append("Um_clockwise_8")
    auxiliary_names.append("Um_clockwise_9")
    auxiliary_names.append("Um_clockwise_10")
    auxiliary_names.append("Um_clockwise_11")
    auxiliary_names.append("Um_clockwise_12")
    auxiliary_names.append("Um_clockwise_13")
    auxiliary_names.append("Um_clockwise_14")

    return auxiliary_names


# 获取列表中的位置函数
def getIndexOfList(str_list, str):
    for i in range(len(str_list)):
        if str == str_list[i]:
            return i
    return -1


# 初始化辅助点
def initAuxiliarys(landmark_x, landmark_y, landmark_names):
    auxiliary_x = []
    auxiliary_y = []

    # auxiliary_x的顺序要和auxiliary_names的顺序保持一致
    idxOrder = getIndexOfList(landmark_names, 'Ns')
    curLX = landmark_x[idxOrder]
    curLY = landmark_y[idxOrder]
    auxiliary_x.append(landmark_x[getIndexOfList(landmark_names, 'G')] - 2)
    auxiliary_y.append(landmark_y[getIndexOfList(landmark_names, 'G')] + 1)
    auxiliary_x.append(curLX + 18)
    auxiliary_y.append(curLY + 99)
    auxiliary_x.append(curLX + 63)
    auxiliary_y.append(curLY + 176)
    auxiliary_x.append((landmark_x[getIndexOfList(landmark_names, 'Prn')] + landmark_x[getIndexOfList(landmark_names, 'Sn')]) / 2)
    auxiliary_y.append((landmark_y[getIndexOfList(landmark_names, 'Prn')] + landmark_y[getIndexOfList(landmark_names, 'Sn')]) / 2 + 10)
    auxiliary_x.append((landmark_x[getIndexOfList(landmark_names, 'Ls')] + landmark_x[getIndexOfList(landmark_names, 'UI')]) / 2)
    auxiliary_y.append((landmark_y[getIndexOfList(landmark_names, 'Ls')] + landmark_y[getIndexOfList(landmark_names, 'UI')]) / 2 + 15)
    auxiliary_x.append(landmark_x[getIndexOfList(landmark_names, 'C')] - 10)
    auxiliary_y.append(landmark_y[getIndexOfList(landmark_names, 'C')] + 10)

    idxOrder = getIndexOfList(landmark_names, "N")
    curLX = landmark_x[idxOrder]
    curLY = landmark_y[idxOrder]
    # N_up
    auxiliary_x.append(curLX + 14)
    auxiliary_y.append(curLY - 180)
    # N_up_add
    auxiliary_x.append(curLX + 24)
    auxiliary_y.append(curLY - 103)
    # N_left
    auxiliary_x.append(curLX - 100)
    auxiliary_y.append(curLY + 50)
    # N_left_down_middle
    auxiliary_x.append(curLX - 71)
    auxiliary_y.append(curLY + 87)
    # N_down_before
    auxiliary_x.append(curLX + 12)
    auxiliary_y.append(curLY + 143)
    # N_down
    auxiliary_x.append(curLX + 85)
    auxiliary_y.append(curLY + 185)
    # N_down_middle
    auxiliary_x.append(curLX + 9)
    auxiliary_y.append(curLY + 90)
    # N_down_middle_2
    auxiliary_x.append(curLX - 1)
    auxiliary_y.append(curLY + 1)

    idxOrder = getIndexOfList(landmark_names, "Or")
    curLX = landmark_x[idxOrder]
    curLY = landmark_y[idxOrder]
    auxiliary_x.append(curLX - 17)
    auxiliary_y.append(curLY - 230)
    auxiliary_x.append(curLX - 37)
    auxiliary_y.append(curLY - 165)
    auxiliary_x.append(curLX - 49)
    auxiliary_y.append(curLY - 100)
    auxiliary_x.append(curLX - 38)
    auxiliary_y.append(curLY - 50)
    auxiliary_x.append(curLX + 30)
    auxiliary_y.append(curLY - 18)
    auxiliary_x.append(curLX + 40)
    auxiliary_y.append(curLY - 35)

    idxSec = getIndexOfList(landmark_names, "Pt")
    curLX = (landmark_x[idxSec] + curLX) / 2 + 20
    curLY = (landmark_y[idxSec] + curLY) / 2
    auxiliary_x.append(curLX + 46)
    auxiliary_y.append(curLY - 200)
    auxiliary_x.append(curLX + 22)
    auxiliary_y.append(curLY - 160)
    auxiliary_x.append(curLX + 5)
    auxiliary_y.append(curLY - 120)
    auxiliary_x.append(curLX + 9)
    auxiliary_y.append(curLY - 60)
    auxiliary_x.append(curLX + 11)
    auxiliary_y.append(curLY)
    auxiliary_x.append(curLX - 5)
    auxiliary_y.append(curLY + 80)
    auxiliary_x.append(curLX - 1)
    auxiliary_y.append(curLY + 141)
    auxiliary_x.append(curLX + 20)
    auxiliary_y.append(curLY + 132)
    auxiliary_x.append(curLX + 43)
    auxiliary_y.append(curLY + 102)

    idxOrder = getIndexOfList(landmark_names, "S")
    curLX = landmark_x[idxOrder]
    curLY = landmark_y[idxOrder]
    auxiliary_x.append(curLX + 360) # S_up
    auxiliary_y.append(curLY - 200)
    auxiliary_x.append(curLX + 255) # S_up_mid_mid
    auxiliary_y.append(curLY - 137)
    auxiliary_x.append(curLX + 150) # S_mid
    auxiliary_y.append(curLY - 80)
    auxiliary_x.append(curLX + 92.5) # S_mid_short_right_mid
    auxiliary_y.append(curLY - 51)
    auxiliary_x.append(curLX + 255) # S_mid_right_mid
    auxiliary_y.append(curLY - 81)
    auxiliary_x.append(curLX + 336) # S_right_before
    auxiliary_y.append(curLY - 78)
    auxiliary_x.append(curLX + 360) # S_right
    auxiliary_y.append(curLY - 90)
    auxiliary_x.append(curLX + 35) # S_short_right
    auxiliary_y.append(curLY)
    auxiliary_x.append(curLX) # S_short_down
    auxiliary_y.append(curLY + 35)
    auxiliary_x.append(curLX - 31) # S_short_left
    auxiliary_y.append(curLY - 5)
    auxiliary_x.append(curLX - 40) # S_short_left_up
    auxiliary_y.append(curLY - 15)
    auxiliary_x.append(curLX - 56) # S_short_left_left
    auxiliary_y.append(curLY + 1)
    # S_left_Ba_mid
    auxiliary_x.append((landmark_x[getIndexOfList(landmark_names, "Ba")] + curLX - 48) / 2)
    auxiliary_y.append((landmark_y[getIndexOfList(landmark_names, "Ba")] + curLY - 10) / 2)
    # S_left_Ba_before
    auxiliary_x.append(landmark_x[getIndexOfList(landmark_names, "Ba")] - 10)
    auxiliary_y.append(landmark_y[getIndexOfList(landmark_names, "Ba")] - 6)

    idxOrder = getIndexOfList(landmark_names, "Ar")
    curLX = landmark_x[idxOrder]
    curLY = landmark_y[idxOrder]
    auxiliary_x.append(curLX + 380) # Ar_up
    auxiliary_y.append(curLY - 420)
    auxiliary_x.append(curLX + 379) # Ar_up_mid_mid
    auxiliary_y.append(curLY - 377)
    auxiliary_x.append(curLX + 340) # Ar_mid
    auxiliary_y.append(curLY - 260)
    # Ar_mid_down
    auxiliary_x.append(landmark_x[getIndexOfList(landmark_names, "Ar")] + 150)
    auxiliary_y.append(landmark_y[getIndexOfList(landmark_names, "Ar")] - 109)
    # Ba_up
    auxiliary_x.append(landmark_x[getIndexOfList(landmark_names, "Ba")] + 15)
    auxiliary_y.append(landmark_y[getIndexOfList(landmark_names, "Ba")] - 9)

    # Co_after
    auxiliary_x.append(landmark_x[getIndexOfList(landmark_names, "Co")] - 8)
    auxiliary_y.append(landmark_y[getIndexOfList(landmark_names, "Co")] - 1)
    # Co_Pcd_mid
    auxiliary_x.append((landmark_x[getIndexOfList(landmark_names, "Co")] + landmark_x[getIndexOfList(landmark_names, "Pcd")]) / 2 - 11)
    auxiliary_y.append((landmark_y[getIndexOfList(landmark_names, "Co")] + landmark_y[getIndexOfList(landmark_names, "Pcd")]) / 2 + 1)
    auxiliary_x.append((landmark_x[getIndexOfList(landmark_names, "Ar")] + landmark_x[getIndexOfList(landmark_names, "Go")]) / 2 + 15)
    auxiliary_y.append((landmark_y[getIndexOfList(landmark_names, "Ar")] + landmark_y[getIndexOfList(landmark_names, "Go")]) / 2)
    auxiliary_x.append((landmark_x[getIndexOfList(landmark_names, "Go")] + landmark_x[getIndexOfList(landmark_names, "Me")]) / 2 - 45)
    auxiliary_y.append((landmark_y[getIndexOfList(landmark_names, "Go")] + landmark_y[getIndexOfList(landmark_names, "Me")]) / 2)
    auxiliary_x.append((landmark_x[getIndexOfList(landmark_names, "Go")] + landmark_x[getIndexOfList(landmark_names, "Me")]) / 2)
    auxiliary_y.append((landmark_y[getIndexOfList(landmark_names, "Go")] + landmark_y[getIndexOfList(landmark_names, "Me")]) / 2 + 15)
    auxiliary_x.append(landmark_x[getIndexOfList(landmark_names, "Me")] - 12)
    auxiliary_y.append(landmark_y[getIndexOfList(landmark_names, "Me")] + 3)

    # Pt - Ptm曲线
    idxOrder = getIndexOfList(landmark_names, "Pt")
    idxSec = getIndexOfList(landmark_names, "Ptm")
    curLX = landmark_x[idxOrder]
    curLY = landmark_y[idxOrder]
    auxiliary_x.append(curLX + 30)
    auxiliary_y.append(curLY - 25)
    auxiliary_x.append((curLX + landmark_x[idxSec]) / 2)
    auxiliary_y.append((curLY + landmark_y[idxSec]) / 2)
    auxiliary_x.append(landmark_x[idxSec])
    auxiliary_y.append((curLY + landmark_y[idxSec] * 2) / 3)
    auxiliary_x.append(curLX + 28)
    auxiliary_y.append(curLY)
    auxiliary_x.append((curLX + landmark_x[idxSec]) / 2 + 10)
    auxiliary_y.append((curLY + landmark_y[idxSec]) / 2)
    auxiliary_x.append(landmark_x[idxSec] + 5)
    auxiliary_y.append((curLY + landmark_y[idxSec] * 2) / 3)

    # Co - Ptm曲线
    idxOrder = getIndexOfList(landmark_names, "Co")
    curLX = landmark_x[idxOrder]
    curLY = landmark_y[idxOrder]
    # Co_right_before
    auxiliary_x.append(landmark_x[getIndexOfList(landmark_names, "Co")] + 8)
    auxiliary_y.append(landmark_y[getIndexOfList(landmark_names, "Co")] - 1)
    # Co_right
    auxiliary_x.append(curLX + 47.5)
    auxiliary_y.append(curLY + 40)
    # Co_right_mid_mid
    auxiliary_x.append(curLX + 47.5)
    auxiliary_y.append(curLY + 90)
    # Co_mid
    auxiliary_x.append(curLX + 92)
    auxiliary_y.append(curLY + 140)
    # Co_mid_down_mid
    auxiliary_x.append(curLX + 132.5)
    auxiliary_y.append(curLY + 180)
    # Co_down
    auxiliary_x.append(curLX + 147.5)
    auxiliary_y.append(curLY + 185)
    # Co_down_Ptm_mid
    auxiliary_x.append((landmark_x[getIndexOfList(landmark_names, "Ptm")] + curLX + 120) / 2 + 12)
    auxiliary_y.append((landmark_y[getIndexOfList(landmark_names, "Ptm")] + curLY + 200) / 2 + 2)
    # Ptm_up_left_before
    auxiliary_x.append(landmark_x[getIndexOfList(landmark_names, "Ptm")] + 17)
    auxiliary_y.append(landmark_y[getIndexOfList(landmark_names, "Ptm")] - 23)

    # Ptm右竖曲线
    idxOrder = getIndexOfList(landmark_names, "Ptm")
    curLX = landmark_x[idxOrder]
    curLY = landmark_y[idxOrder]
    # Ptm_up
    auxiliary_x.append(curLX + 30)
    auxiliary_y.append(curLY - 30)
    # Ptm_up_mid
    auxiliary_x.append(curLX + 11)
    auxiliary_y.append(curLY + 20)
    # Ptm_mid
    auxiliary_x.append(curLX - 3)
    auxiliary_y.append(curLY + 80)
    # Ptm_mid_down_mid
    auxiliary_x.append(curLX - 5)
    auxiliary_y.append(curLY + 140)
    # Ptm_down
    auxiliary_x.append(curLX)
    auxiliary_y.append(curLY + 200)

    # PNS - ANS曲线 & PNS曲线 & ANS - Spr曲线
    idxOrder = getIndexOfList(landmark_names, "PNS")
    curLX = landmark_x[idxOrder]
    curLY = landmark_y[idxOrder]
    # PNS_after
    auxiliary_x.append(curLX + 16)
    auxiliary_y.append(curLY - 6)
    # PNS_right_before
    auxiliary_x.append(curLX + 100)
    auxiliary_y.append(curLY - 7)
    # PNS_right
    auxiliary_x.append(curLX + 176)
    auxiliary_y.append(curLY - 10)
    # PNS_ANS_top_before
    auxiliary_x.append((landmark_x[getIndexOfList(landmark_names, "ANS")] + curLX + 120) / 2 - 5)
    auxiliary_y.append((landmark_y[getIndexOfList(landmark_names, "ANS")] + curLY) / 2 - 35)
    # PNS_ANS_top
    auxiliary_x.append((landmark_x[getIndexOfList(landmark_names, "ANS")] + curLX + 120) / 2 + 10)
    auxiliary_y.append((landmark_y[getIndexOfList(landmark_names, "ANS")] + curLY) / 2 - 40)
    # ANS_before
    auxiliary_x.append(landmark_x[getIndexOfList(landmark_names, "ANS")] - 5)
    auxiliary_y.append(landmark_y[getIndexOfList(landmark_names, "ANS")] - 3)
    # PNS_down_after
    auxiliary_x.append(curLX + 11)
    auxiliary_y.append(curLY + 6)
    # PNS_mid_before
    auxiliary_x.append(curLX + 100)
    auxiliary_y.append(curLY + 18.1)
    # PNS_mid
    auxiliary_x.append(curLX + 200)
    auxiliary_y.append(curLY + 40)
    # PNS_mid_down_mid
    auxiliary_x.append(curLX + 262)
    auxiliary_y.append(curLY + 88)
    # PNS_down_before
    auxiliary_x.append(curLX + 305)
    auxiliary_y.append(curLY + 134)
    # PNS_down
    auxiliary_x.append(curLX + 310)
    auxiliary_y.append(curLY + 140)
    idxOrder = getIndexOfList(landmark_names, "ANS")
    # ANS_left
    auxiliary_x.append(landmark_x[idxOrder] - 75)
    auxiliary_y.append(landmark_y[idxOrder] - 20)
    # ANS_after
    auxiliary_x.append(landmark_x[idxOrder] - 6)
    auxiliary_y.append(landmark_y[idxOrder] + 11.5)
    # ANS_A_mid
    auxiliary_x.append((landmark_x[idxOrder] + landmark_x[getIndexOfList(landmark_names, "A")]) / 2)
    auxiliary_y.append((landmark_y[idxOrder] + landmark_y[getIndexOfList(landmark_names, "A")]) / 2)
    # A_Spr_mid
    auxiliary_x.append((landmark_x[getIndexOfList(landmark_names, "A")] + landmark_x[getIndexOfList(landmark_names, "Spr")]) / 2 - 4)
    auxiliary_y.append((landmark_y[getIndexOfList(landmark_names, "A")] + landmark_y[getIndexOfList(landmark_names, "Spr")]) / 2)

    idxOrder = getIndexOfList(landmark_names, "Me")
    curLX = landmark_x[idxOrder]
    curLY = landmark_y[idxOrder]
    # Me_up
    auxiliary_x.append(curLX + 40)
    auxiliary_y.append(curLY - 250)
    # Me_up_after
    auxiliary_x.append(curLX + 36)
    auxiliary_y.append(curLY - 229.9)
    # Me_mid
    auxiliary_x.append(curLX - 30)
    auxiliary_y.append(curLY - 140)
    # Me_left
    auxiliary_x.append(curLX - 34)
    auxiliary_y.append(curLY - 50)
    # Me_before_left
    auxiliary_x.append(curLX - 6)
    auxiliary_y.append(curLY - 3)

    # Id_after
    auxiliary_x.append(landmark_x[getIndexOfList(landmark_names, "Id")] - 3)
    auxiliary_y.append(landmark_y[getIndexOfList(landmark_names, "Id")] + 3)
    # Id_B_mid
    auxiliary_x.append((landmark_x[getIndexOfList(landmark_names, "Id")] + landmark_x[getIndexOfList(landmark_names, "B")]) / 2 - 5)
    auxiliary_y.append((landmark_y[getIndexOfList(landmark_names, "Id")] + landmark_y[getIndexOfList(landmark_names, "B")]) / 2)
    # Me_before_right
    auxiliary_x.append(landmark_x[getIndexOfList(landmark_names, "Me")] + 7.3)
    auxiliary_y.append(landmark_y[getIndexOfList(landmark_names, "Me")] - 1)

    idxOrder = getIndexOfList(landmark_names, "LIA")
    idxSec = getIndexOfList(landmark_names, "Id")
    curLX = landmark_x[idxOrder]
    curLY = landmark_y[idxOrder]
    auxiliary_x.append((landmark_x[getIndexOfList(landmark_names, "LI")] + landmark_x[idxSec]) / 2 + 9)
    auxiliary_y.append((landmark_y[getIndexOfList(landmark_names, "LI")] + landmark_y[idxSec]) / 2 - 17)
    auxiliary_x.append((curLX + landmark_x[idxSec]) / 2 + 5)
    auxiliary_y.append((curLY + landmark_y[idxSec]) / 2 + 5)
    auxiliary_x.append(curLX + 17)
    auxiliary_y.append(curLY - 11)
    auxiliary_x.append(curLX - 6)
    auxiliary_y.append(curLY - 11)
    auxiliary_x.append(curLX - 2)
    auxiliary_y.append((curLY + landmark_y[idxSec]) / 2)
    auxiliary_x.append(landmark_x[idxSec] - 65)
    auxiliary_y.append(landmark_y[idxSec] + 15)
    auxiliary_x.append(landmark_x[idxSec] - 55)
    auxiliary_y.append(landmark_y[idxSec] - 10)
    auxiliary_x.append(landmark_x[idxSec] - 25)
    auxiliary_y.append((landmark_y[getIndexOfList(landmark_names, "LI")] + landmark_y[idxSec] + 11) / 2 - 6)
    auxiliary_x.append(landmark_x[getIndexOfList(landmark_names, "LI")] - 11)
    auxiliary_y.append(landmark_y[getIndexOfList(landmark_names, "LI")] + 7)

    idxOrder = getIndexOfList(landmark_names, "UIA")
    idxSec = getIndexOfList(landmark_names, "Spr")
    curLX = landmark_x[idxOrder]
    curLY = landmark_y[idxOrder]
    auxiliary_x.append(landmark_x[getIndexOfList(landmark_names, "UI")] + 4.6)
    auxiliary_y.append(landmark_y[getIndexOfList(landmark_names, "UI")] - 9)
    auxiliary_x.append((curLX + landmark_x[idxSec]) / 2 + 5)
    auxiliary_y.append((curLY + landmark_y[idxSec]) / 2 - 1)
    auxiliary_x.append(curLX + 15)
    auxiliary_y.append(curLY + 6)
    auxiliary_x.append(curLX - 5)
    auxiliary_y.append(curLY + 5)
    auxiliary_x.append(curLX)
    auxiliary_y.append((curLY + landmark_y[idxSec]) / 2)
    auxiliary_x.append(landmark_x[idxSec] - 49)
    auxiliary_y.append(landmark_y[idxSec] + 26)
    auxiliary_x.append(landmark_x[idxSec] - 40)
    auxiliary_y.append(landmark_y[idxSec] + 45)
    auxiliary_x.append((landmark_x[getIndexOfList(landmark_names, "UI")] + landmark_x[idxSec]) / 2 - 22)
    auxiliary_y.append((landmark_y[getIndexOfList(landmark_names, "UI")] + landmark_y[idxSec]) / 2 + 15)
    auxiliary_x.append(landmark_x[getIndexOfList(landmark_names, "UI")] - 10)
    auxiliary_y.append(landmark_y[getIndexOfList(landmark_names, "UI")] - 5)

    idxOrder = getIndexOfList(landmark_names, "Lm")
    curLX = landmark_x[idxOrder]
    curLY = landmark_y[idxOrder]
    auxiliary_x.append(curLX + 12)
    auxiliary_y.append(curLY + 15)
    auxiliary_x.append(curLX + 5)
    auxiliary_y.append(curLY + 45)
    auxiliary_x.append(curLX - 12)
    auxiliary_y.append(curLY + 109)
    auxiliary_x.append(curLX - 35)
    auxiliary_y.append(curLY + 130)
    auxiliary_x.append(curLX - 37)
    auxiliary_y.append(curLY + 117)
    auxiliary_x.append(curLX - 30)
    auxiliary_y.append(curLY + 78)
    auxiliary_x.append(curLX - 58)
    auxiliary_y.append(curLY + 100)
    auxiliary_x.append(curLX - 80)
    auxiliary_y.append(curLY + 115)
    auxiliary_x.append(curLX - 86)
    auxiliary_y.append(curLY + 97)
    auxiliary_x.append(curLX - 77)
    auxiliary_y.append(curLY + 76)
    auxiliary_x.append(curLX - 70)
    auxiliary_y.append(curLY + 45)
    auxiliary_x.append(curLX - 72)
    auxiliary_y.append(curLY + 10)
    auxiliary_x.append(curLX - 60)
    auxiliary_y.append(curLY - 9)
    auxiliary_x.append(curLX - 40)
    auxiliary_y.append(curLY)
    auxiliary_x.append(curLX - 25)
    auxiliary_y.append(curLY + 10)

    idxOrder = getIndexOfList(landmark_names, "Um")
    curLX = landmark_x[idxOrder]
    curLY = landmark_y[idxOrder]
    auxiliary_x.append(curLX - 25)
    auxiliary_y.append(curLY - 20)
    auxiliary_x.append(curLX - 25)
    auxiliary_y.append(curLY - 5)
    auxiliary_x.append(curLX - 52)
    auxiliary_y.append(curLY - 1)
    auxiliary_x.append(curLX - 70)
    auxiliary_y.append(curLY - 9)
    auxiliary_x.append(curLX - 65)
    auxiliary_y.append(curLY - 70)
    auxiliary_x.append(curLX - 69)
    auxiliary_y.append(curLY - 100)
    auxiliary_x.append(curLX - 58)
    auxiliary_y.append(curLY - 130)
    auxiliary_x.append(curLX - 45)
    auxiliary_y.append(curLY - 110)
    auxiliary_x.append(curLX - 33)
    auxiliary_y.append(curLY - 81)
    auxiliary_x.append(curLX - 25)
    auxiliary_y.append(curLY - 111)
    auxiliary_x.append(curLX - 22)
    auxiliary_y.append(curLY - 130)
    auxiliary_x.append(curLX - 5)
    auxiliary_y.append(curLY - 110)
    auxiliary_x.append(curLX)
    auxiliary_y.append(curLY - 100)
    auxiliary_x.append(curLX + 7.5)
    auxiliary_y.append(curLY - 65)
    auxiliary_x.append(curLX + 15)
    auxiliary_y.append(curLY - 10)

    return auxiliary_x, auxiliary_y


# 初始化轮廓
def initCurve(landmark_x, landmark_y, landmark_names, auxiliary_x, auxiliary_y, auxiliary_names):
    CurveNum = 22
    idxOrder = []
    idxOrderName =[]
    points = []
    pointsName = []
    parPoints = {}
    parPointsName = {}

    for curveIdx in range(CurveNum):
        points.clear()
        pointsName.clear()
        idxOrder.clear()
        idxOrderName.clear()
        if curveIdx == 0:
            idxOrder.append(getIndexOfList(landmark_names, "G"))
            idxOrder.append(getIndexOfList(auxiliary_names, "G_Ns_middle"))
            idxOrder.append(getIndexOfList(landmark_names, "Ns"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Ns_down"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Prn_left_up"))
            idxOrder.append(getIndexOfList(landmark_names, "Prn"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Prn_Sn_middle"))
            idxOrder.append(getIndexOfList(landmark_names, "Sn"))
            idxOrder.append(getIndexOfList(landmark_names, "Ls"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Ls_UI_middle"))
            idxOrder.append(getIndexOfList(landmark_names, "UI"))
            idxOrder.append(getIndexOfList(landmark_names, "Li"))
            idxOrder.append(getIndexOfList(landmark_names, "Si"))
            idxOrder.append(getIndexOfList(landmark_names, "Pogs"))
            idxOrder.append(getIndexOfList(landmark_names, "Gns"))
            idxOrder.append(getIndexOfList(landmark_names, "Mes"))
            idxOrder.append(getIndexOfList(landmark_names, "C"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Mes_C_middle"))

            idxOrderName.append("G")
            idxOrderName.append("G_Ns_middle")
            idxOrderName.append("Ns")
            idxOrderName.append("Ns_down")
            idxOrderName.append("Prn_left_up")
            idxOrderName.append("Prn")
            idxOrderName.append("Prn_Sn_middle")
            idxOrderName.append("Sn")
            idxOrderName.append("Ls")
            idxOrderName.append("Ls_UI_middle")
            idxOrderName.append("UI")
            idxOrderName.append("Li")
            idxOrderName.append("Si")
            idxOrderName.append("Pogs")
            idxOrderName.append("Gns")
            idxOrderName.append("Mes")
            idxOrderName.append("C")
            idxOrderName.append("Mes_C_middle")

            for i in range(len(idxOrder)):
                pointsName.append(idxOrderName[i])
                if i == 1 or i == 3 or i == 4 or i == 6 or i == 9 or i == 17:
                    points.append([auxiliary_x[idxOrder[i]], auxiliary_y[idxOrder[i]]])
                else:
                    points.append([landmark_x[idxOrder[i]], landmark_y[idxOrder[i]]])
            points_copy = copy.deepcopy(points)
            pointsName_copy = copy.deepcopy(pointsName)

        elif curveIdx == 1:
            idxOrder.append(getIndexOfList(auxiliary_names, "N_up"))
            idxOrder.append(getIndexOfList(auxiliary_names, "N_up_add"))
            idxOrder.append(getIndexOfList(landmark_names, "N"))
            idxOrder.append(getIndexOfList(auxiliary_names, "N_left"))
            idxOrder.append(getIndexOfList(auxiliary_names, "N_left_down_middle"))
            idxOrder.append(getIndexOfList(auxiliary_names, "N_down_before"))
            idxOrder.append(getIndexOfList(auxiliary_names, "N_down"))

            idxOrderName.append("N_up")
            idxOrderName.append("N_up_add")
            idxOrderName.append("N")
            idxOrderName.append("N_left")
            idxOrderName.append("N_left_down_middle")
            idxOrderName.append("N_down_before")
            idxOrderName.append("N_down")

            for i in range(len(idxOrder)):
                pointsName.append(idxOrderName[i])
                if i == 2:
                    points.append([landmark_x[idxOrder[i]], landmark_y[idxOrder[i]]])
                else:
                    points.append([auxiliary_x[idxOrder[i]], auxiliary_y[idxOrder[i]]])
            points_copy = copy.deepcopy(points)
            pointsName_copy = copy.deepcopy(pointsName)

        elif curveIdx == 2:
            idxOrder.append(getIndexOfList(landmark_names, "N"))
            idxOrder.append(getIndexOfList(auxiliary_names, "N_down_middle_2"))
            idxOrder.append(getIndexOfList(auxiliary_names, "N_down_middle"))
            idxOrder.append(getIndexOfList(auxiliary_names, "N_down"))

            idxOrderName.append("N")
            idxOrderName.append("N_down_middle_2")
            idxOrderName.append("N_down_middle")
            idxOrderName.append("N_down")

            for i in range(len(idxOrder)):
                pointsName.append(idxOrderName[i])
                if i == 0:
                    points.append([landmark_x[idxOrder[i]], landmark_y[idxOrder[i]]])
                else:
                    points.append([auxiliary_x[idxOrder[i]], auxiliary_y[idxOrder[i]]])
            points_copy = copy.deepcopy(points)
            pointsName_copy = copy.deepcopy(pointsName)

        elif curveIdx == 3:
            idxOrder.append(getIndexOfList(auxiliary_names, "Or_up"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Or_up_left_middle"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Or_left"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Or_left_middle"))
            idxOrder.append(getIndexOfList(landmark_names, "Or"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Or_right_middle"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Or_right"))

            idxOrderName.append("Or_up")
            idxOrderName.append("Or_up_left_middle")
            idxOrderName.append("Or_left")
            idxOrderName.append("Or_left_middle")
            idxOrderName.append("Or")
            idxOrderName.append("Or_right_middle")
            idxOrderName.append("Or_right")

            for i in range(len(idxOrder)):
                pointsName.append(idxOrderName[i])
                if i == 4:
                    points.append([landmark_x[idxOrder[i]], landmark_y[idxOrder[i]]])
                else:
                    points.append([auxiliary_x[idxOrder[i]], auxiliary_y[idxOrder[i]]])
            points_copy = copy.deepcopy(points)
            pointsName_copy = copy.deepcopy(pointsName)

        elif curveIdx == 4:
            idxOrder.append(getIndexOfList(auxiliary_names, "midPtOr_up"))
            idxOrder.append(getIndexOfList(auxiliary_names, "midPtOr_up_mid_mid"))
            idxOrder.append(getIndexOfList(auxiliary_names, "midPtOr_mid"))
            idxOrder.append(getIndexOfList(auxiliary_names, "midPtOr_mid_midPtOr_mid"))
            idxOrder.append(getIndexOfList(auxiliary_names, "midPtOr"))
            idxOrder.append(getIndexOfList(auxiliary_names, "midPtOr_down_mid"))
            idxOrder.append(getIndexOfList(auxiliary_names, "midPtOr_down"))
            idxOrder.append(getIndexOfList(auxiliary_names, "midPtOr_down_right_mid"))
            idxOrder.append(getIndexOfList(auxiliary_names, "midPtOr_right"))

            idxOrderName.append("midPtOr_up")
            idxOrderName.append("midPtOr_up_mid_mid")
            idxOrderName.append("midPtOr_mid")
            idxOrderName.append("midPtOr_mid_midPtOr_mid")
            idxOrderName.append("midPtOr")
            idxOrderName.append("midPtOr_down_mid")
            idxOrderName.append("midPtOr_down")
            idxOrderName.append("midPtOr_down_right_mid")
            idxOrderName.append("midPtOr_right")

            for i in range(len(idxOrder)):
                pointsName.append(idxOrderName[i])
                points.append([auxiliary_x[idxOrder[i]], auxiliary_y[idxOrder[i]]])
            points_copy = copy.deepcopy(points)
            pointsName_copy = copy.deepcopy(pointsName)

        elif curveIdx == 5:
            idxOrder.append(getIndexOfList(landmark_names, "Po"))

            pointsName.append("Po")
            points.append([landmark_x[idxOrder[0]], landmark_y[idxOrder[0]]])
            points_copy = copy.deepcopy(points)
            pointsName_copy = copy.deepcopy(pointsName)

        elif curveIdx == 6:
            idxOrder.append(getIndexOfList(auxiliary_names, "S_mid"))
            idxOrder.append(getIndexOfList(auxiliary_names, "S_mid_right_mid"))
            idxOrder.append(getIndexOfList(auxiliary_names, "S_right_before"))
            idxOrder.append(getIndexOfList(auxiliary_names, "S_right"))

            idxOrderName.append("S_mid")
            idxOrderName.append("S_mid_right_mid")
            idxOrderName.append("S_right_before")
            idxOrderName.append("S_right")

            for i in range(len(idxOrder)):
                pointsName.append(idxOrderName[i])
                points.append([auxiliary_x[idxOrder[i]], auxiliary_y[idxOrder[i]]])
            points_copy = copy.deepcopy(points)
            pointsName_copy = copy.deepcopy(pointsName)

        elif curveIdx == 7:
            idxOrder.append(getIndexOfList(auxiliary_names, "S_up"))
            idxOrder.append(getIndexOfList(auxiliary_names, "S_up_mid_mid"))
            idxOrder.append(getIndexOfList(auxiliary_names, "S_mid"))
            idxOrder.append(getIndexOfList(auxiliary_names, "S_mid_short_right_mid"))
            idxOrder.append(getIndexOfList(auxiliary_names, "S_short_right"))
            idxOrder.append(getIndexOfList(auxiliary_names, "S_short_down"))
            idxOrder.append(getIndexOfList(auxiliary_names, "S_short_left"))
            idxOrder.append(getIndexOfList(auxiliary_names, "S_short_left_up"))
            idxOrder.append(getIndexOfList(auxiliary_names, "S_short_left_left"))
            idxOrder.append(getIndexOfList(auxiliary_names, "S_left_Ba_mid"))
            idxOrder.append(getIndexOfList(auxiliary_names, "S_left_Ba_before"))
            idxOrder.append(getIndexOfList(landmark_names, "Ba"))

            idxOrderName.append("S_up")
            idxOrderName.append("S_up_mid_mid")
            idxOrderName.append("S_mid")
            idxOrderName.append("S_mid_short_right_mid")
            idxOrderName.append("S_short_right")
            idxOrderName.append("S_short_down")
            idxOrderName.append("S_short_left")
            idxOrderName.append("S_short_left_up")
            idxOrderName.append("S_short_left_left")
            idxOrderName.append("S_left_Ba_mid")
            idxOrderName.append("S_left_Ba_before")
            idxOrderName.append("Ba")

            for i in range(len(idxOrder)):
                pointsName.append(idxOrderName[i])
                if i == 11:
                    points.append([landmark_x[idxOrder[i]], landmark_y[idxOrder[i]]])
                else:
                    points.append([auxiliary_x[idxOrder[i]], auxiliary_y[idxOrder[i]]])
            points_copy = copy.deepcopy(points)
            pointsName_copy = copy.deepcopy(pointsName)

        elif curveIdx == 8:
            idxOrder.append(getIndexOfList(auxiliary_names, "Ar_up"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Ar_up_mid_mid"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Ar_mid"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Ar_mid_down"))
            idxOrder.append(getIndexOfList(landmark_names, "Ar"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Ba_up"))
            idxOrder.append(getIndexOfList(landmark_names, "Ba"))

            idxOrderName.append("Ar_up")
            idxOrderName.append("Ar_up_mid_mid")
            idxOrderName.append("Ar_mid")
            idxOrderName.append("Ar_mid_down")
            idxOrderName.append("Ar")
            idxOrderName.append("Ba_up")
            idxOrderName.append("Ba")

            for i in range(len(idxOrder)):
                pointsName.append(idxOrderName[i])
                if i == 4 or i == 6:
                    points.append([landmark_x[idxOrder[i]], landmark_y[idxOrder[i]]])
                else:
                    points.append([auxiliary_x[idxOrder[i]], auxiliary_y[idxOrder[i]]])
            points_copy = copy.deepcopy(points)
            pointsName_copy = copy.deepcopy(pointsName)

        elif curveIdx == 9:
            idxOrder.append(getIndexOfList(landmark_names, "Co"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Co_after"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Co_Pcd_mid"))
            idxOrder.append(getIndexOfList(landmark_names, "Pcd"))
            idxOrder.append(getIndexOfList(landmark_names, "Ar"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Ar_Go_mid"))
            idxOrder.append(getIndexOfList(landmark_names, "Go"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Go_Me_mid"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Go_Me_mid_2"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Me_before"))
            idxOrder.append(getIndexOfList(landmark_names, "Me"))

            idxOrderName.append("Co")
            idxOrderName.append("Co_after")
            idxOrderName.append("Co_Pcd_mid")
            idxOrderName.append("Pcd")
            idxOrderName.append("Ar")
            idxOrderName.append("Ar_Go_mid")
            idxOrderName.append("Go")
            idxOrderName.append("Go_Me_mid")
            idxOrderName.append("Go_Me_mid_2")
            idxOrderName.append("Me_before")
            idxOrderName.append("Me")

            for i in range(len(idxOrder)):
                pointsName.append(idxOrderName[i])
                if i == 0 or i == 3 or i == 4 or i == 6 or i == 10:
                    points.append([landmark_x[idxOrder[i]], landmark_y[idxOrder[i]]])
                else:
                    points.append([auxiliary_x[idxOrder[i]], auxiliary_y[idxOrder[i]]])
            points_copy = copy.deepcopy(points)
            pointsName_copy = copy.deepcopy(pointsName)

        elif curveIdx == 10:
            idxOrder.append(getIndexOfList(auxiliary_names, "Pt_up"))
            idxOrder.append(getIndexOfList(landmark_names, "Pt"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Pt_mid"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Pt_down"))
            idxOrder.append(getIndexOfList(landmark_names, "Ptm"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Pt_right_down"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Pt_right_mid"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Pt_right"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Pt_up"))

            idxOrderName.append("Pt_up")
            idxOrderName.append("Pt")
            idxOrderName.append("Pt_mid")
            idxOrderName.append("Pt_down")
            idxOrderName.append("Ptm")
            idxOrderName.append("Pt_right_down")
            idxOrderName.append("Pt_right_mid")
            idxOrderName.append("Pt_right")
            idxOrderName.append("Pt_up")

            for i in range(len(idxOrder)):
                pointsName.append(idxOrderName[i])
                if i == 1 or i == 4:
                    points.append([landmark_x[idxOrder[i]], landmark_y[idxOrder[i]]])
                else:
                    points.append([auxiliary_x[idxOrder[i]], auxiliary_y[idxOrder[i]]])
            points_copy = copy.deepcopy(points)
            pointsName_copy = copy.deepcopy(pointsName)

        elif curveIdx == 11:
            idxOrder.append(getIndexOfList(landmark_names, "Co"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Co_right_before"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Co_right"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Co_right_mid_mid"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Co_mid"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Co_mid_down_mid"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Co_down"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Co_down_Ptm_mid"))
            idxOrder.append(getIndexOfList(landmark_names, "Ptm"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Ptm_up_left_before"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Ptm_up"))

            idxOrderName.append("Co")
            idxOrderName.append("Co_right_before")
            idxOrderName.append("Co_right")
            idxOrderName.append("Co_right_mid_mid")
            idxOrderName.append("Co_mid")
            idxOrderName.append("Co_mid_down_mid")
            idxOrderName.append("Co_down")
            idxOrderName.append("Co_down_Ptm_mid")
            idxOrderName.append("Ptm")
            idxOrderName.append("Ptm_up_left_before")
            idxOrderName.append("Ptm_up")

            for i in range(len(idxOrder)):
                pointsName.append(idxOrderName[i])
                if i == 0 or i == 8:
                    points.append([landmark_x[idxOrder[i]], landmark_y[idxOrder[i]]])
                else:
                    points.append([auxiliary_x[idxOrder[i]], auxiliary_y[idxOrder[i]]])
            points_copy = copy.deepcopy(points)
            pointsName_copy = copy.deepcopy(pointsName)

        elif curveIdx == 12:
            idxOrder.append(getIndexOfList(auxiliary_names, "Ptm_up"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Ptm_up_mid"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Ptm_mid"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Ptm_mid_down_mid"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Ptm_down"))

            idxOrderName.append("Ptm_up")
            idxOrderName.append("Ptm_up_mid")
            idxOrderName.append("Ptm_mid")
            idxOrderName.append("Ptm_mid_down_mid")
            idxOrderName.append("Ptm_down")

            for i in range(len(idxOrder)):
                pointsName.append(idxOrderName[i])
                points.append([auxiliary_x[idxOrder[i]], auxiliary_y[idxOrder[i]]])
            points_copy = copy.deepcopy(points)
            pointsName_copy = copy.deepcopy(pointsName)

        elif curveIdx == 13:
            idxOrder.append(getIndexOfList(landmark_names, "PNS"))
            idxOrder.append(getIndexOfList(auxiliary_names, "PNS_after"))
            idxOrder.append(getIndexOfList(auxiliary_names, "PNS_right_before"))
            idxOrder.append(getIndexOfList(auxiliary_names, "PNS_right"))
            idxOrder.append(getIndexOfList(auxiliary_names, "PNS_ANS_top_before"))
            idxOrder.append(getIndexOfList(auxiliary_names, "PNS_ANS_top"))
            idxOrder.append(getIndexOfList(auxiliary_names, "ANS_left"))
            idxOrder.append(getIndexOfList(auxiliary_names, "ANS_before"))
            idxOrder.append(getIndexOfList(landmark_names, "ANS"))

            idxOrderName.append("PNS")
            idxOrderName.append("PNS_after")
            idxOrderName.append("PNS_right_before")
            idxOrderName.append("PNS_right")
            idxOrderName.append("PNS_ANS_top_before")
            idxOrderName.append("PNS_ANS_top")
            idxOrderName.append("ANS_left")
            idxOrderName.append("ANS_before")
            idxOrderName.append("ANS")

            for i in range(len(idxOrder)):
                pointsName.append(idxOrderName[i])
                if i == 0 or i == 8:
                    points.append([landmark_x[idxOrder[i]], landmark_y[idxOrder[i]]])
                else:
                    points.append([auxiliary_x[idxOrder[i]], auxiliary_y[idxOrder[i]]])
            points_copy = copy.deepcopy(points)
            pointsName_copy = copy.deepcopy(pointsName)

        elif curveIdx == 14:
            idxOrder.append(getIndexOfList(landmark_names, "PNS"))
            idxOrder.append(getIndexOfList(auxiliary_names, "PNS_down_after"))
            idxOrder.append(getIndexOfList(auxiliary_names, "PNS_mid_before"))
            idxOrder.append(getIndexOfList(auxiliary_names, "PNS_mid"))
            idxOrder.append(getIndexOfList(auxiliary_names, "PNS_mid_down_mid"))
            idxOrder.append(getIndexOfList(auxiliary_names, "PNS_down_before"))
            idxOrder.append(getIndexOfList(auxiliary_names, "PNS_down"))

            idxOrderName.append("PNS")
            idxOrderName.append("PNS_down_after")
            idxOrderName.append("PNS_mid_before")
            idxOrderName.append("PNS_mid")
            idxOrderName.append("PNS_mid_down_mid")
            idxOrderName.append("PNS_down_before")
            idxOrderName.append("PNS_down")

            for i in range(len(idxOrder)):
                pointsName.append(idxOrderName[i])
                if i == 0:
                    points.append([landmark_x[idxOrder[i]], landmark_y[idxOrder[i]]])
                else:
                    points.append([auxiliary_x[idxOrder[i]], auxiliary_y[idxOrder[i]]])
            points_copy = copy.deepcopy(points)
            pointsName_copy = copy.deepcopy(pointsName)

        elif curveIdx == 15:
            idxOrder.append(getIndexOfList(landmark_names, "ANS"))
            idxOrder.append(getIndexOfList(auxiliary_names, "ANS_after"))
            idxOrder.append(getIndexOfList(auxiliary_names, "ANS_A_mid"))
            idxOrder.append(getIndexOfList(landmark_names, "A"))
            idxOrder.append(getIndexOfList(auxiliary_names, "A_Spr_mid"))
            idxOrder.append(getIndexOfList(landmark_names, "Spr"))

            idxOrderName.append("ANS")
            idxOrderName.append("ANS_after")
            idxOrderName.append("ANS_A_mid")
            idxOrderName.append("A")
            idxOrderName.append("A_Spr_mid")
            idxOrderName.append("Spr")

            for i in range(len(idxOrder)):
                pointsName.append(idxOrderName[i])
                if i == 0 or i == 3 or i == 5:
                    points.append([landmark_x[idxOrder[i]], landmark_y[idxOrder[i]]])
                else:
                    points.append([auxiliary_x[idxOrder[i]], auxiliary_y[idxOrder[i]]])
            points_copy = copy.deepcopy(points)
            pointsName_copy = copy.deepcopy(pointsName)

        elif curveIdx == 16:
            idxOrder.append(getIndexOfList(auxiliary_names, "Me_up"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Me_up_after"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Me_mid"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Me_left"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Me_before_left"))
            idxOrder.append(getIndexOfList(landmark_names, "Me"))

            idxOrderName.append("Me_up")
            idxOrderName.append("Me_up_after")
            idxOrderName.append("Me_mid")
            idxOrderName.append("Me_left")
            idxOrderName.append("Me_before_left")
            idxOrderName.append("Me")

            for i in range(len(idxOrder)):
                pointsName.append(idxOrderName[i])
                if i == 5:
                    points.append([landmark_x[idxOrder[i]], landmark_y[idxOrder[i]]])
                else:
                    points.append([auxiliary_x[idxOrder[i]], auxiliary_y[idxOrder[i]]])
            points_copy = copy.deepcopy(points)
            pointsName_copy = copy.deepcopy(pointsName)

        elif curveIdx == 17:
            idxOrder.append(getIndexOfList(landmark_names, "Id"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Id_after"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Id_B_mid"))
            idxOrder.append(getIndexOfList(landmark_names, "B"))
            idxOrder.append(getIndexOfList(landmark_names, "Pog"))
            idxOrder.append(getIndexOfList(landmark_names, "Gn"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Me_before_right"))
            idxOrder.append(getIndexOfList(landmark_names, "Me"))

            idxOrderName.append("Id")
            idxOrderName.append("Id_after")
            idxOrderName.append("Id_B_mid")
            idxOrderName.append("B")
            idxOrderName.append("Pog")
            idxOrderName.append("Gn")
            idxOrderName.append("Me_before_right")
            idxOrderName.append("Me")

            for i in range(len(idxOrder)):
                pointsName.append(idxOrderName[i])
                if i == 1 or i == 2 or i == 6:
                    points.append([auxiliary_x[idxOrder[i]], auxiliary_y[idxOrder[i]]])
                else:
                    points.append([landmark_x[idxOrder[i]], landmark_y[idxOrder[i]]])
            points_copy = copy.deepcopy(points)
            pointsName_copy = copy.deepcopy(pointsName)

        elif curveIdx == 18:
            idxOrder.append(getIndexOfList(landmark_names, "LI"))
            idxOrder.append(getIndexOfList(auxiliary_names, "LI_after"))
            idxOrder.append(getIndexOfList(landmark_names, "Id"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Id_after"))
            idxOrder.append(getIndexOfList(auxiliary_names, "LIA_before"))
            idxOrder.append(getIndexOfList(landmark_names, "LIA"))
            idxOrder.append(getIndexOfList(auxiliary_names, "LIA_left"))
            idxOrder.append(getIndexOfList(auxiliary_names, "LIA_Id_mid"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Id_left"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Id_left_up"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Id_LI_mid"))
            idxOrder.append(getIndexOfList(auxiliary_names, "LI_before"))
            idxOrder.append(getIndexOfList(landmark_names, "LI"))

            idxOrderName.append("LI")
            idxOrderName.append("LI_after")
            idxOrderName.append("Id")
            idxOrderName.append("Id_after")
            idxOrderName.append("LIA_before")
            idxOrderName.append("LIA")
            idxOrderName.append("LIA_left")
            idxOrderName.append("LIA_Id_mid")
            idxOrderName.append("Id_left")
            idxOrderName.append("Id_left_up")
            idxOrderName.append("Id_LI_mid")
            idxOrderName.append("LI_before")
            idxOrderName.append("LI")

            for i in range(len(idxOrder)):
                pointsName.append(idxOrderName[i])
                if i == 0 or i == 2 or i == 5 or i == 12:
                    points.append([landmark_x[idxOrder[i]], landmark_y[idxOrder[i]]])
                else:
                    points.append([auxiliary_x[idxOrder[i]], auxiliary_y[idxOrder[i]]])
            points_copy = copy.deepcopy(points)
            pointsName_copy = copy.deepcopy(pointsName)

        elif curveIdx == 19:
            idxOrder.append(getIndexOfList(landmark_names, "UI"))
            idxOrder.append(getIndexOfList(auxiliary_names, "UI_after"))
            idxOrder.append(getIndexOfList(landmark_names, "Spr"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Spr_after"))
            idxOrder.append(getIndexOfList(auxiliary_names, "UIA_before"))
            idxOrder.append(getIndexOfList(landmark_names, "UIA"))
            idxOrder.append(getIndexOfList(auxiliary_names, "UIA_left"))
            idxOrder.append(getIndexOfList(auxiliary_names, "UIA_Spr_mid"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Spr_left"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Spr_left_down"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Spr_UI_mid"))
            idxOrder.append(getIndexOfList(auxiliary_names, "UI_before"))
            idxOrder.append(getIndexOfList(landmark_names, "UI"))

            idxOrderName.append("UI")
            idxOrderName.append("UI_after")
            idxOrderName.append("Spr")
            idxOrderName.append("Spr_after")
            idxOrderName.append("UIA_before")
            idxOrderName.append("UIA")
            idxOrderName.append("UIA_left")
            idxOrderName.append("UIA_Spr_mid")
            idxOrderName.append("Spr_left")
            idxOrderName.append("Spr_left_down")
            idxOrderName.append("Spr_UI_mid")
            idxOrderName.append("UI_before")
            idxOrderName.append("UI")

            for i in range(len(idxOrder)):
                pointsName.append(idxOrderName[i])
                if i == 0 or i == 2 or i == 5 or i == 12:
                    points.append([landmark_x[idxOrder[i]], landmark_y[idxOrder[i]]])
                else:
                    points.append([auxiliary_x[idxOrder[i]], auxiliary_y[idxOrder[i]]])
            points_copy = copy.deepcopy(points)
            pointsName_copy = copy.deepcopy(pointsName)

        elif curveIdx == 20:
            idxOrder.append(getIndexOfList(landmark_names, "Lm"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Lm_clockwise_0"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Lm_clockwise_1"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Lm_clockwise_2"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Lm_clockwise_3"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Lm_clockwise_4"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Lm_clockwise_5"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Lm_clockwise_6"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Lm_clockwise_7"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Lm_clockwise_8"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Lm_clockwise_9"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Lm_clockwise_10"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Lm_clockwise_11"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Lm_clockwise_12"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Lm_clockwise_13"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Lm_clockwise_14"))
            idxOrder.append(getIndexOfList(landmark_names, "Lm"))

            idxOrderName.append("Lm")
            idxOrderName.append("Lm_clockwise_0")
            idxOrderName.append("Lm_clockwise_1")
            idxOrderName.append("Lm_clockwise_2")
            idxOrderName.append("Lm_clockwise_3")
            idxOrderName.append("Lm_clockwise_4")
            idxOrderName.append("Lm_clockwise_5")
            idxOrderName.append("Lm_clockwise_6")
            idxOrderName.append("Lm_clockwise_7")
            idxOrderName.append("Lm_clockwise_8")
            idxOrderName.append("Lm_clockwise_9")
            idxOrderName.append("Lm_clockwise_10")
            idxOrderName.append("Lm_clockwise_11")
            idxOrderName.append("Lm_clockwise_12")
            idxOrderName.append("Lm_clockwise_13")
            idxOrderName.append("Lm_clockwise_14")
            idxOrderName.append("Lm")

            for i in range(len(idxOrder)):
                pointsName.append(idxOrderName[i])
                if i == 0 or i == 16:
                    points.append([landmark_x[idxOrder[i]], landmark_y[idxOrder[i]]])
                else:
                    points.append([auxiliary_x[idxOrder[i]], auxiliary_y[idxOrder[i]]])
            points_copy = copy.deepcopy(points)
            pointsName_copy = copy.deepcopy(pointsName)

        elif curveIdx == 21:
            idxOrder.append(getIndexOfList(auxiliary_names, "Um_clockwise_0"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Um_clockwise_1"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Um_clockwise_2"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Um_clockwise_3"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Um_clockwise_4"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Um_clockwise_5"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Um_clockwise_6"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Um_clockwise_7"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Um_clockwise_8"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Um_clockwise_9"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Um_clockwise_10"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Um_clockwise_11"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Um_clockwise_12"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Um_clockwise_13"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Um_clockwise_14"))
            idxOrder.append(getIndexOfList(landmark_names, "Um"))
            idxOrder.append(getIndexOfList(auxiliary_names, "Um_clockwise_1"))

            idxOrderName.append("Um_clockwise_0")
            idxOrderName.append("Um_clockwise_1")
            idxOrderName.append("Um_clockwise_2")
            idxOrderName.append("Um_clockwise_3")
            idxOrderName.append("Um_clockwise_4")
            idxOrderName.append("Um_clockwise_5")
            idxOrderName.append("Um_clockwise_6")
            idxOrderName.append("Um_clockwise_7")
            idxOrderName.append("Um_clockwise_8")
            idxOrderName.append("Um_clockwise_9")
            idxOrderName.append("Um_clockwise_10")
            idxOrderName.append("Um_clockwise_11")
            idxOrderName.append("Um_clockwise_12")
            idxOrderName.append("Um_clockwise_13")
            idxOrderName.append("Um_clockwise_14")
            idxOrderName.append("Um")
            idxOrderName.append("Um_clockwise_1")

            for i in range(len(idxOrder)):
                pointsName.append(idxOrderName[i])
                if i == 15:
                    points.append([landmark_x[idxOrder[i]], landmark_y[idxOrder[i]]])
                else:
                    points.append([auxiliary_x[idxOrder[i]], auxiliary_y[idxOrder[i]]])
            points_copy = copy.deepcopy(points)
            pointsName_copy = copy.deepcopy(pointsName)

        parPoints[curveIdx] = points_copy
        parPointsName[curveIdx] = pointsName_copy

    return parPoints, parPointsName


'''画特征点'''
def drawPoints(ImageDict, Image, color):
    color_arr = [0, 0, 0]
    if color == 0:
        color_arr = (255, 0, 0)
    elif color == 1:
        color_arr = (0, 0, 255)
    for [m, n] in ImageDict.values():
        cv2.circle(Image, (m, n), 3, color_arr, -1, cv2.FILLED)


'''画轮廓'''
# 得到两点之间的距离
def get_dis(dis1, dis2):
    v = (dis1[0] - dis2[0]) * (dis1[0] - dis2[0]) + (dis1[1] - dis2[1]) * (dis1[1] - dis2[1])

    return math.sqrt(v)

# 获取每个顶点对应的两个控制点
def get_bezier_control_points(point_list, smooth_value=1):
    ctrl_list = [point_list[0]]

    for i, value in enumerate(point_list):
        if i == 0:
            continue
        if i == len(point_list) - 1:
            continue

        a_x, a_y = point_list[i - 1]
        b_x, b_y = value
        c_x, c_y = point_list[i + 1]

        ab_x = (a_x + b_x) / 2.0
        ab_y = (a_y + b_y) / 2.0

        bc_x = (b_x + c_x) / 2.0
        bc_y = (b_y + c_y) / 2.0

        len1 = get_dis(point_list[i - 1], value)
        len2 = get_dis(point_list[i + 1], value)

        k = len1 / (len1 + len2)

        d_x = ab_x + (bc_x - ab_x) * k
        d_y = ab_y + (bc_y - ab_y) * k

        ctrl0_x = b_x + (ab_x - d_x) * smooth_value
        ctrl0_y = b_y + (ab_y - d_y) * smooth_value

        ctrl1_x = b_x + (bc_x - d_x) * smooth_value
        ctrl1_y = b_y + (bc_y - d_y) * smooth_value

        ctrl_list.append([ctrl0_x, ctrl0_y])
        ctrl_list.append([ctrl1_x, ctrl1_y])

    ctrl_list.append(point_list[-1])

    return ctrl_list

# 画线
def drawOutlines(points, Image, color):
    result = get_bezier_control_points(points)
    # multi_points用于保存所有特征点+辅助点+控制点信息
    multi_points = []
    for i, p in enumerate(points):
        if i == 0:
            multi_points.append(p)
            continue

        multi_points.append(result[(i - 1) * 2])
        multi_points.append(result[(i - 1) * 2 + 1])
        multi_points.append(p)

    # 循环标志
    num = 0
    # 颜色处理
    color_arr = [0, 0, 0]
    if color == 0:
        color_arr = (255, 0, 0)
    elif color == 1:
        color_arr = (0, 0, 255)

    while(num < len(multi_points) - 1):
        # 每次取两个点和它们之间的两个控制点
        divice_points = np.int32(multi_points[num:num+4])
        # 提取出x和y坐标
        arr_x = []
        arr_y = []
        for i, values in enumerate(divice_points):
            arr_x.append(values[0])
            arr_y.append(values[1])
        arr_points = np.array([arr_x, arr_y])
        # 贝塞尔曲线处理
        curve = bezier.Curve(arr_points, degree=3)
        s_vals = np.linspace(0.0, 1.0, 50)
        data = curve.evaluate_multi(s_vals)
        multi_x = data[0]
        multi_y = data[1]

        marge_points = []
        for i in range(len(multi_x)):
            marge_points.append([multi_x[i], multi_y[i]])

        # 作图
        pts = np.array(marge_points, np.int32)
        pts = pts.reshape((-1, 1, 2))
        cv2.polylines(Image, [pts], False, color_arr, 2)

        num += 3

# 将base64编码转换为cv2格式，image_type为图像类型（0代表黑白，1代表彩色）
def base64_to_cv2(base64_code, image_type):
    img_data = base64.b64decode(base64_code)
    img_array = np.frombuffer(img_data, np.uint8)
    image = cv2.imdecode(img_array, image_type)
    return image

# 将cv2格式转换为base64编码
def cv2_to_base64(image):
    base64_str = cv2.imencode('.jpg', image)[1].tostring()
    base64_str = base64.b64encode(base64_str)
    base64_str = base64_str.decode()
    base64_str = 'data:image/jpeg;base64,' + base64_str
    return base64_str



'''智能定点'''
def fixedPoint(base64_code, image_type):
    image = base64_to_cv2(base64_code, 1)
    image = np.uint8(image)

    imageDict, imageLandmark_names, imageLandmark_x, imageLandmark_y = getLandmarks(base64_code)
    # image_type决定图片类型：0为参考图像，1为浮动图像
    drawPoints(imageDict, image, image_type)
    afterFixedPointBase64 = cv2_to_base64(image)

    return afterFixedPointBase64



'''绘制轮廓'''
def fixedOutlines(base64_code, image_type):
    image = base64_to_cv2(base64_code, 1)
    image = np.uint8(image)

    # 获取数据
    imageDict, imageLandmark_names, imageLandmark_x, imageLandmark_y = getLandmarks(base64_code)
    auxiliary_names = getAuxiliarys()
    imageAuxiliary_x, imageAuxiliary_y = initAuxiliarys(imageLandmark_x, imageLandmark_y, imageLandmark_names)
    imageParPoints, imageParPointsName = initCurve(
        imageLandmark_x,
        imageLandmark_y,
        imageLandmark_names,
        imageAuxiliary_x,
        imageAuxiliary_y,
        auxiliary_names)

    # 画轮廓
    for i in range(len(imageParPoints)):
        image_PointsData = imageParPoints[i]
        drawOutlines(image_PointsData, image, image_type)

    # 只绘制了轮廓的图像
    afterFixedOutlinesBase64 = cv2_to_base64(image)

    # 定点+绘制轮廓的图像
    drawPoints(imageDict, image, image_type)
    afterPointAndOutlinesBase64 = cv2_to_base64(image)

    return afterFixedOutlinesBase64, afterPointAndOutlinesBase64



'''配准'''
def registrate(fixed_base64_code, moving_base64_code):
    # 参考图像
    fixedImage1 = base64_to_cv2(fixed_base64_code, 1)
    fixedImage1 = np.uint8(fixedImage1)
    fixedImage2 = base64_to_cv2(fixed_base64_code, 1)
    fixedImage2 = np.uint8(fixedImage2)
    fixedImage3 = base64_to_cv2(fixed_base64_code, 1)
    fixedImage3 = np.uint8(fixedImage3)
    fixedImage4 = base64_to_cv2(fixed_base64_code, 1)
    fixedImage4 = np.uint8(fixedImage4)
    # 浮动图像
    movingImage1 = base64_to_cv2(moving_base64_code, 1)
    movingImage1 = np.uint8(movingImage1)
    movingImage2 = base64_to_cv2(moving_base64_code, 1)
    movingImage2 = np.uint8(movingImage2)
    movingImage3 = base64_to_cv2(moving_base64_code, 1)
    movingImage3 = np.uint8(movingImage3)
    movingImage4 = base64_to_cv2(moving_base64_code, 1)
    movingImage4 = np.uint8(movingImage4)

    # 得到待配准图像
    fixedImageDict, fixedImageLandmark_names, fixedImageLandmark_x, fixedImageLandmark_y = getLandmarks(fixed_base64_code)
    movingImageDict, movingImageLandmark_names, movingImageLandmark_x, movingImageLandmark_y = getLandmarks(moving_base64_code)

    # 获取数据
    auxiliary_names = getAuxiliarys()
    # 参考图像
    fixedImageAuxiliary_x, fixedImageAuxiliary_y = initAuxiliarys(fixedImageLandmark_x, fixedImageLandmark_y, fixedImageLandmark_names)
    fixedImageParPoints, fixedImageParPointsName = initCurve(
        fixedImageLandmark_x,
        fixedImageLandmark_y,
        fixedImageLandmark_names,
        fixedImageAuxiliary_x,
        fixedImageAuxiliary_y,
        auxiliary_names)
    # 浮动图像
    movingImageAuxiliary_x, movingImageAuxiliary_y = initAuxiliarys(movingImageLandmark_x, movingImageLandmark_y, movingImageLandmark_names)
    movingImageParPoints, movingImageParPointsName = initCurve(
        movingImageLandmark_x,
        movingImageLandmark_y,
        movingImageLandmark_names,
        movingImageAuxiliary_x,
        movingImageAuxiliary_y,
        auxiliary_names)



    # 【利用OpenCV自带的SIFT方法提取特征点并计算变换矩阵】
    sift = cv2.SIFT_create()

    # 检测图片（计算图像的关键点和特征向量）
    kp1, des1 = sift.detectAndCompute(fixedImage1, None)
    kp2, des2 = sift.detectAndCompute(movingImage1, None)

    # 获取FLANN匹配器
    FLANN_INDEX_KDTREE = 0
    # 参数1：indexParams
    #    对于SIFT和SURF，可以传入参数index_params=dict(algorithm=FLANN_INDEX_KDTREE, trees=5)。
    #    对于ORB，可以传入参数index_params=dict(algorithm=FLANN_INDEX_LSH, table_number=6, key_size=12）。
    indexParams = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
    # 参数2：searchParams 指定递归遍历的次数，值越高结果越准确，但是消耗的时间也越多。
    searchParams = dict(checks=50)


    # 使用FlannBasedMatcher 寻找最近邻近似匹配
    flann = cv2.FlannBasedMatcher(indexParams, searchParams)
    # 使用knnMatch匹配处理，并返回匹配matches
    matches = flann.knnMatch(des1, des2, k=2)
    # 通过掩码方式计算有用的点
    matchesMask = [[0, 0] for i in range(len(matches))]

    # 通过描述符的距离进行选择需要的点
    # 这里使用过的kNN匹配的k值为2（在训练集中找两个点），第一个匹配的是最近邻，第二个匹配的是次近邻。
    # 换句话说，一个不正确的匹配，两个邻居的距离是相似的。因此，我们可以通过比值检测来评判距匹配程度的好坏。
    # 若第一个匹配和第二个匹配的比值小于一个给定的值（一般是0.5），就认为可以取第一个最近邻的值。
    goodMatch3 = []
    for i, (m, n) in enumerate(matches):
        if m.distance < 0.5 * n.distance:  # 通过系数来决定匹配的有效关键点数量
            matchesMask[i] = [1, 0]
            goodMatch3.append(m)


    # ptsA ptsB 为关键点
    ptsA = np.float32([kp1[m.queryIdx].pt for m in goodMatch3]).reshape(-1, 1, 2)
    ptsB = np.float32([kp2[m.trainIdx].pt for m in goodMatch3]).reshape(-1, 1, 2)

    # 四自由度的仿射变换
    M1, inliers1 = cv2.estimateAffinePartial2D(ptsA, ptsB)

    # 【情况一：原始配准图像】
    outputImage1 = cv2.warpAffine(movingImage1, M1, (fixedImage1.shape[1], fixedImage1.shape[0]), flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP)
    overlapping1 = cv2.addWeighted(fixedImage1, 0.5, outputImage1, 0.5, 0)
    original_image = cv2_to_base64(overlapping1)

    # 【情况二：带特征点的配准图像】
    drawPoints(fixedImageDict, fixedImage2, 0)
    drawPoints(movingImageDict, movingImage2, 1)
    outputImage2 = cv2.warpAffine(movingImage2, M1, (fixedImage2.shape[1], fixedImage2.shape[0]), flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP)
    overlapping2 = cv2.addWeighted(fixedImage2, 0.5, outputImage2, 0.5, 0)
    point_image = cv2_to_base64(overlapping2)

    # 【情况三：带轮廓的配准图像】
    for i in range(len(fixedImageParPoints)):
        fixedImage_PointsData = fixedImageParPoints[i]
        drawOutlines(fixedImage_PointsData, fixedImage3, 0)
    for i in range(len(movingImageParPoints)):
        movingImage_PointsData = movingImageParPoints[i]
        drawOutlines(movingImage_PointsData, movingImage3, 1)
    outputImage3 = cv2.warpAffine(movingImage3, M1, (fixedImage3.shape[1], fixedImage3.shape[0]), flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP)
    overlapping3 = cv2.addWeighted(fixedImage3, 0.5, outputImage3, 0.5, 0)
    outlines_image = cv2_to_base64(overlapping3)

    # 【情况四：完整的配准图像】
    drawPoints(fixedImageDict, fixedImage4, 0)
    drawPoints(movingImageDict, movingImage4, 1)
    for i in range(len(fixedImageParPoints)):
        fixedImage_PointsData = fixedImageParPoints[i]
        drawOutlines(fixedImage_PointsData, fixedImage4, 0)
    for i in range(len(movingImageParPoints)):
        movingImage_PointsData = movingImageParPoints[i]
        drawOutlines(movingImage_PointsData, movingImage4, 1)
    outputImage4 = cv2.warpAffine(movingImage4, M1, (fixedImage4.shape[1], fixedImage4.shape[0]), flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP)
    overlapping4 = cv2.addWeighted(fixedImage4, 0.5, outputImage4, 0.5, 0)
    complete_image = cv2_to_base64(overlapping4)

    return original_image, point_image, outlines_image, complete_image


# # 读入参考和浮动图像（以彩色图像读入）
# fixedImage = base64_to_cv2(base64_code, 1)
# fixedImage = np.uint8(fixedImage)
# fixedImageCopy = cv2.imread(fixedImagePath, 0)
#
# movingImage = cv2.imread(movingImagePath, 1)
# movingImage = np.uint8(movingImage)
# movingImageCopy = cv2.imread(movingImagePath, 0)
#
#
# # 得到待配准图像
# fixedImageDict, fixedImageLandmark_names, fixedImageLandmark_x, fixedImageLandmark_y = getLandmarks(fixedImagePath)
# movingImageDict, movingImageLandmark_names, movingImageLandmark_x, movingImageLandmark_y = getLandmarks(movingImagePath)
#
# # 获取数据
# auxiliary_names = getAuxiliarys()
# # 参考图像
# fixedImageAuxiliary_x, fixedImageAuxiliary_y = initAuxiliarys(fixedImageLandmark_x, fixedImageLandmark_y, fixedImageLandmark_names)
# fixedImageParPoints, fixedImageParPointsName = initCurve(
#     fixedImageLandmark_x,
#     fixedImageLandmark_y,
#     fixedImageLandmark_names,
#     fixedImageAuxiliary_x,
#     fixedImageAuxiliary_y,
#     auxiliary_names)
# # 浮动图像
# movingImageAuxiliary_x, movingImageAuxiliary_y = initAuxiliarys(movingImageLandmark_x, movingImageLandmark_y, movingImageLandmark_names)
# movingImageParPoints, movingImageParPointsName = initCurve(
#     movingImageLandmark_x,
#     movingImageLandmark_y,
#     movingImageLandmark_names,
#     movingImageAuxiliary_x,
#     movingImageAuxiliary_y,
#     auxiliary_names)
#
#
#
# # 【方法一：利用OpenCV自带的SIFT方法提取特征点并计算变换矩阵】
# sift = cv2.SIFT_create()
#
# # 检测图片（计算图像的关键点和特征向量）
# kp1, des1 = sift.detectAndCompute(fixedImage, None)
# kp2, des2 = sift.detectAndCompute(movingImage, None)
#
# # 获取FLANN匹配器
# FLANN_INDEX_KDTREE = 0
# # 参数1：indexParams
# #    对于SIFT和SURF，可以传入参数index_params=dict(algorithm=FLANN_INDEX_KDTREE, trees=5)。
# #    对于ORB，可以传入参数index_params=dict(algorithm=FLANN_INDEX_LSH, table_number=6, key_size=12）。
# indexParams = dict(algorithm=FLANN_INDEX_KDTREE, trees=5)
# # 参数2：searchParams 指定递归遍历的次数，值越高结果越准确，但是消耗的时间也越多。
# searchParams = dict(checks=50)
#
#
# # 使用FlannBasedMatcher 寻找最近邻近似匹配
# flann = cv2.FlannBasedMatcher(indexParams, searchParams)
# # 使用knnMatch匹配处理，并返回匹配matches
# matches = flann.knnMatch(des1, des2, k=2)
# # 通过掩码方式计算有用的点
# matchesMask = [[0, 0] for i in range(len(matches))]
#
# # 通过描述符的距离进行选择需要的点
# # 这里使用过的kNN匹配的k值为2（在训练集中找两个点），第一个匹配的是最近邻，第二个匹配的是次近邻。
# # 换句话说，一个不正确的匹配，两个邻居的距离是相似的。因此，我们可以通过比值检测来评判距匹配程度的好坏。
# # 若第一个匹配和第二个匹配的比值小于一个给定的值（一般是0.5），就认为可以取第一个最近邻的值。
# goodMatch3 = []
# for i, (m, n) in enumerate(matches):
#     if m.distance < 0.5 * n.distance:  # 通过系数来决定匹配的有效关键点数量
#         matchesMask[i] = [1, 0]
#         goodMatch3.append(m)
#
#
# # ptsA ptsB 为关键点
# ptsA = np.float32([kp1[m.queryIdx].pt for m in goodMatch3]).reshape(-1, 1, 2)
# ptsB = np.float32([kp2[m.trainIdx].pt for m in goodMatch3]).reshape(-1, 1, 2)
#
# # 四自由度的仿射变换
# M1, inliers1 = cv2.estimateAffinePartial2D(ptsA, ptsB)
#
#
# '''在图中画特征点'''
# drawPoints(fixedImageDict, fixedImage, 0)
# drawPoints(movingImageDict, movingImage, 1)
#
#
# '''在图中画轮廓'''
# for i in range(len(fixedImageParPoints)):
#     fixedImage_PointsData = fixedImageParPoints[i]
#     drawOutlines(fixedImage_PointsData, fixedImage, 0)
# for i in range(len(movingImageParPoints)):
#     movingImage_PointsData = movingImageParPoints[i]
#     drawOutlines(movingImage_PointsData, movingImage, 1)
#
#
# # 输出图像
# # cv2.warpAffine(输入图像，变换矩阵，输出图像大小，插值方法)用于仿射变换
# OutputImage1 = cv2.warpAffine(
#     movingImage,
#     M1,
#     (fixedImage.shape[1], fixedImage.shape[0]),
#     flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP
# )
# OutputImage1_WithoutPoint = cv2.warpAffine(
#     movingImageCopy,
#     M1,
#     (fixedImageCopy.shape[1], fixedImageCopy.shape[0]),
#     flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP
# )
#
# # 叠加配准变换图和基准图
# rate = 0.5
# # cv2.addWeighted(第一个Mat，第一个Mat的权重，第二个Mat，第二个Mat的权重，加权和后图像的亮度调整)
# overlapping1 = cv2.addWeighted(fixedImage, rate, OutputImage1, 1-rate, 0)
# # cv2.absdiff()获取差分图
# diff1 = cv2.absdiff(fixedImage, OutputImage1)
#
#
# # 显示
# plt.title('方法一IMGOUT', fontsize=20)
# plt.axis('off')
# plt.imshow(OutputImage1)
# plt.show()
#
# plt.title('方法一OVERLAPPING', fontsize=20)
# plt.axis('off')
# plt.imshow(overlapping1)
# plt.show()
# cv2.imwrite('D:/Desktop/Overlapping1.JPG', overlapping1)
#
# plt.title('方法一DIFF', fontsize=20)
# plt.axis('off')
# plt.imshow(diff1)
# plt.show()
#
#
# # 相似性测度
# print('【方法一】')
#
# # 取四个特征点S、Po、PNS、Or
# fixed_S = fixedImageDict.get('S')
# fixed_Po = fixedImageDict.get('Po')
# fixed_PNS = fixedImageDict.get('PNS')
# fixed_Or = fixedImageDict.get('Or')
#
# # 求局部矩形区域的左上角和右下角坐标
# left_up_x = fixed_Po[0]
# left_up_y = fixed_S[1]
# right_down_x = fixed_Or[0]
# right_down_y = fixed_PNS[1]
#
# # 利用互信息计算相似性
# x1_Global = np.reshape(fixedImageCopy, -1)
# y1_Global = np.reshape(OutputImage1_WithoutPoint, -1)
# print('【全局互信息值】：', MI(x1_Global, y1_Global))
# x1_Local = np.reshape(fixedImageCopy[left_up_x:right_down_x, left_up_y:right_down_y], -1)
# y1_Local = np.reshape(OutputImage1_WithoutPoint[left_up_x:right_down_x, left_up_y:right_down_y], -1)
# print('【局部互信息值】：', MI(x1_Local, y1_Local))
#
# # 利用均方误差计算相似性
# print('【全局均方误差】：', MSE(fixedImageCopy, OutputImage1_WithoutPoint))
# print('【局部均方误差】：', MSE(fixedImageCopy[left_up_x:right_down_x, left_up_y:right_down_y], OutputImage1_WithoutPoint[left_up_x:right_down_x, left_up_y:right_down_y]))
#
# # 利用结构相似性指数计算相似性
# print('【结构相似性指数】：', ssim(fixedImageCopy, OutputImage1_WithoutPoint, multichannel=True))
